package com.bawei.week3.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.bawei.week3.R;
import com.bawei.week3.entity.PictureEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

public class PicAdapter extends BaseAdapter {
    private List<PictureEntity> list;
    private Context context;

    public PicAdapter(List<PictureEntity> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder viewholder = new Viewholder();
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
            viewholder.imageView = convertView.findViewById(R.id.iv);
            convertView.setTag(viewholder);
        }else{
            viewholder = (Viewholder) convertView.getTag();
        }
        Glide.with(context)
                .load(list.get(position).getData())
                .transform(new CenterCrop())//居中裁剪
                .into(viewholder.imageView);
        return convertView;
    }
    class Viewholder{
        ImageView imageView;
    }
}
